package com.yicoollxing.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.yicoollxing.common.ResponseCode;
import com.yicoollxing.common.ServerResponse;
import com.yicoollxing.dao.mapper.FoodMapper;
import com.yicoollxing.pojo.Food;
import com.yicoollxing.service.IFoodService;
import com.yicoollxing.util.DateTimeUtil;
import com.yicoollxing.util.PropertiesUtil;
import com.yicoollxing.vo.FoodDetialVo;
import com.yicoollxing.vo.FoodListVo;

@Service("iFoodService")
public class FoodServiceImpl implements IFoodService {

	@Autowired
	private FoodMapper foodMapper;

	public ServerResponse modifyAndAddFood(Food food) {

		if (food == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		if (!StringUtils.isEmpty(food.getSubImages())) {
			String[] imageUrls = food.getSubImages().split(",");
			if (imageUrls.length > 0) {
				food.setMainImage(imageUrls[0]);
			}
		}

		// id 为null说明是新增，id不为空为更新
		if (food.getId() != null) {

			food.setUpdateTime(new Date());
			int resultRaw = foodMapper.updateByPrimaryKeySelective(food);
			if (resultRaw == 0) {
				return ServerResponse.createByErrorMessage("更新食物失败");
			} else {
				return ServerResponse.createBySuccessMessage("更新食物成功");
			}
		} else {
			int resultRaw = foodMapper.insertSelective(food);
			if (resultRaw == 0) {
				return ServerResponse.createByErrorMessage("新增食物失败");
			} else {
				return ServerResponse.createBySuccessMessage("新增食物成功");
			}
		}
	}

	public ServerResponse setFoodStatus(Integer foodId, Integer status) {

		if (foodId == null || status == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		if (status != 0 && status != 1) {
			return ServerResponse.createByErrorMessage("操作异常");
		}
		Food food = new Food();
		food.setId(foodId);
		food.setStatus(status);
		food.setUpdateTime(new Date());

		int resultRaw = foodMapper.updateByPrimaryKeySelective(food);
		String resultMsg = null;
		if (resultRaw > 0) {
			if (status == 0) {
				resultMsg = "食物下线成功";
			} else if (status == 1) {
				resultMsg = "食物上线成功";
			}
			return ServerResponse.createBySuccessMessage(resultMsg);
		} else {
			if (status == 0) {
				resultMsg = "食物下线失败";
			} else if (status == 1) {
				resultMsg = "食物上线失败";
			}
			return ServerResponse.createBySuccessMessage(resultMsg);
		}
	}

	public ServerResponse getFoodDetail(Integer foodId) {

		if (foodId == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		Food food = foodMapper.selectByPrimaryKey(foodId);
		if (food == null) {
			return ServerResponse.createByErrorMessage("食物已上架或删除");
		}
		FoodDetialVo foodDetialVo = assembleFoodVo(food);

		return ServerResponse.createBySuccess(foodDetialVo);
	}

	private FoodDetialVo assembleFoodVo(Food food) {
		FoodDetialVo foodDetialVo = new FoodDetialVo();
		foodDetialVo.setId(food.getId());
		foodDetialVo.setMid(food.getMid());
		foodDetialVo.setCategoryId(food.getCategoryId());
		foodDetialVo.setName(food.getName());
		foodDetialVo.setMainImage(food.getMainImage());
		foodDetialVo.setSubImages(food.getSubImages());
		foodDetialVo.setDetail(food.getDetail());
		foodDetialVo.setShortdesc(food.getShortdesc());
		foodDetialVo.setPrice(food.getPrice());
		foodDetialVo.setDiscount(food.getDiscount());
		foodDetialVo.setIsChara(food.getIsChara());
		foodDetialVo.setSum(food.getSum());
		foodDetialVo.setIsTakeOut(food.getIsTakeOut());
		foodDetialVo.setStatus(food.getStatus());
		foodDetialVo
				.setCreateTime(DateTimeUtil.dateToStr(food.getCreateTime()));
		foodDetialVo
				.setUpdateTime(DateTimeUtil.dateToStr(food.getUpdateTime()));

		foodDetialVo.setImageHost(PropertiesUtil
				.getProperty("ftp.server.http.prefix"));
		;
		return foodDetialVo;
	}

	public ServerResponse getFoodList(Integer mid, Integer pageNum,
			Integer pageSize) {

		if (mid == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		PageHelper pageHelper = new PageHelper();
		pageHelper.startPage(pageNum, pageSize);
		List<Food> foodList = foodMapper.selectByMid(mid);
		PageInfo pageInfo = new PageInfo(foodList);
		List<FoodListVo> foodListVoList = Lists.newArrayList();
		for (Food foodItem : foodList) {
			foodListVoList.add(assembleFoodList(foodItem));
		}
		pageInfo.setList(foodListVoList);

		return ServerResponse.createBySuccess(pageInfo);
	}

	private FoodListVo assembleFoodList(Food foodItem) {
		FoodListVo foodListVo = new FoodListVo();
		foodListVo.setId(foodItem.getId());
		foodListVo.setName(foodItem.getName());
		foodListVo.setMainImage(foodItem.getMainImage());
		foodListVo.setShortdesc(foodItem.getShortdesc());
		foodListVo.setPrice(foodItem.getPrice());
		foodListVo.setDiscount(foodItem.getDiscount());
		foodListVo.setIsChara(foodItem.getIsChara());
		foodListVo.setIsTakeOut(foodItem.getIsTakeOut());
		foodListVo.setStatus(foodItem.getStatus());
		
		foodListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));

		return foodListVo;
	}

	public ServerResponse queryFoodByIdOrName(Integer mid, Integer foodId,
			String foodName, Integer pageNum, Integer pageSize) {
		if (mid == null || (foodId == null && StringUtils.isEmpty(foodName))) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		if (StringUtils.isNotBlank(foodName)) {
			foodName = new StringBuilder().append("%").append(foodName)
					.append("%").toString();
		}
		PageHelper pageHelper = new PageHelper();
		pageHelper.startPage(pageNum, pageSize);
		List<Food> foodList = foodMapper.selectByIdAndName(mid, foodId,
				foodName);
		PageInfo pageInfo = new PageInfo(foodList);
		List<FoodListVo> foodListVoList = Lists.newArrayList();
		for (Food foodItem : foodList) {
			foodListVoList.add(assembleFoodList(foodItem));
		}
		pageInfo.setList(foodListVoList);
		return ServerResponse.createBySuccess(pageInfo);
	}
}
